package cc.lj.internal.coupons.application.service;

import cc.lj.internal.api.merchant.dto.PropertyRelatonApiDTO;
import cc.lj.internal.api.merchant.dto.PropertyResourcesApiDTO;
import cc.lj.internal.api.merchant.query.PropertyRelationApiQuery;
import cc.lj.internal.core.model.PageBean;
import cc.lj.internal.core.model.PageParam;
import cc.lj.internal.coupons.application.FeignAppService;
import cc.lj.internal.coupons.domain.martcap.model.vo.CapBatchDO;
import cc.lj.internal.coupons.domain.martcap.model.vo.CapMerchantDO;
import cc.lj.internal.coupons.domain.martcap.model.vo.MultipleModelDO;
import cc.lj.internal.coupons.domain.martcap.model.vo.convert.CapMerchantDOConvertPO;
import cc.lj.internal.coupons.domain.martcap.service.ICapBatchDomainService;
import cc.lj.internal.coupons.domain.martcap.service.ICapMerchantDomainService;
import cc.lj.internal.coupons.domain.martcap.service.IMultipleModelDomainService;
import cc.lj.internal.coupons.domain.ticket.model.vo.TicketBatchDO;
import cc.lj.internal.coupons.domain.ticket.service.ITicketBatchDomainService;
import cc.lj.internal.coupons.infrastructure.po.CapMerchantPO;
import cc.lj.internal.coupons.interfaces.dto.CapBatchDTO;
import cc.lj.internal.coupons.interfaces.dto.CapMerchantDTO;
import cc.lj.internal.coupons.interfaces.dto.MultipleModelDTO;
import cc.lj.internal.coupons.interfaces.dto.TicketBatchDTO;
import cc.lj.internal.coupons.interfaces.dto.command.AddCapMerchantCommand;
import cc.lj.internal.coupons.interfaces.dto.command.InsertCapBatchCommand;
import cc.lj.internal.coupons.interfaces.dto.command.UpdateCapBatchCommand;
import cc.lj.internal.coupons.interfaces.dto.convert.CapBatchDTOConvertDO;
import cc.lj.internal.coupons.interfaces.dto.convert.CapMerchantDTOConvertDO;
import cc.lj.internal.coupons.interfaces.dto.convert.MultipleModelDTOConvertDO;
import cc.lj.internal.coupons.interfaces.dto.convert.TicketBatchDTOConvertDO;
import cc.lj.internal.coupons.interfaces.dto.query.CapBatchQuery;
import cc.lj.internal.coupons.interfaces.dto.query.CapMerchantListQuery;
import cc.lj.internal.coupons.interfaces.dto.query.SelectMerchantCommand;
import lj.internal.dict.enums.EnumPropertyRelationLevel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 封顶额商户表 app service
 * @author liangxifeng
 * @date 2021-12-20
 */
@Service
@Slf4j
public class CapMerchantAppService {
    //封顶额活商户领域层
    @Resource(name = "ICapMerchantDomainServiceImpl")
    private ICapMerchantDomainService capMerchantDomainService;

    //倍数模型领域层
    @Resource(name = "IMultipleModelDomainServiceImpl")
    private IMultipleModelDomainService modelDomainService;

    //通用外网应用层
    @Resource(name = "feignAppService")
    private FeignAppService feignAppService;

    /**
     * 保存活动批次表数据
     * @param addCapMerchantCommandList 新增商户list
     * @return 新增主键 list
     */
    public List<Integer> save(List<AddCapMerchantCommand> addCapMerchantCommandList) {
        List<Integer> lastIds = new ArrayList<>();
        //循环新增
        for (AddCapMerchantCommand addCapMerchantCommand : addCapMerchantCommandList) {
            CapMerchantDO capMerchantDO = CapMerchantDTOConvertDO.INSTANCE.convert(addCapMerchantCommand);
            Integer lastId = capMerchantDomainService.save(capMerchantDO);
            lastIds.add(lastId);
        }
        return lastIds;
    }

    public PageBean<CapMerchantDTO> selectPage(PageParam<CapMerchantListQuery> pageParam) {
        //查询列表
        PageBean<CapMerchantDO> pageBean = capMerchantDomainService.selectPage(convert(pageParam));
        List<CapMerchantDTO> capBatchListDTO = new ArrayList();
        //循环查询商户数据，并赋值到CapMerchantDTO中
        for (CapMerchantDO record : pageBean.getRecords()) {
            //通过主键查查询倍数模型表DO
            MultipleModelDO modelDO = modelDomainService.getById(record.getModelId());
            //转换倍数模型DO为DTO
            MultipleModelDTO modelDTO = MultipleModelDTOConvertDO.INSTANCE.convert(modelDO);
            //转换商户DO to DTO
            CapMerchantDTO capMerchantDTO = CapMerchantDTOConvertDO.INSTANCE.convert(record);
            //封顶额活动DTO中电子券批次DTO属性赋值
            capMerchantDTO.setMultipleModelDTO(modelDTO);
            //拼接最终返回的DTO列表
            capBatchListDTO.add(capMerchantDTO);
        }
        //重新生成pageBean，做返回值
        PageBean pageBeanRes = PageBean.createPageBean(pageBean.getPageNum(),
                pageBean.getPageSize(), pageBean.getTotal(), capBatchListDTO);
        return pageBeanRes;
    }
    /**
     * DO to PO
     * @param pageParam
     * @return
     */
    public PageParam<CapMerchantDO> convert(PageParam<CapMerchantListQuery> pageParam) {
        PageParam<CapMerchantDO> pageParamR = new PageParam<>();
        pageParamR.setPageNum( pageParam.getPageNum() ) ;
        pageParamR.setPageSize( pageParam.getPageSize() );
        pageParamR.setOrderBy( pageParam.getOrderBy() );
        CapMerchantDO capMerchantDO = CapMerchantDTOConvertDO.INSTANCE.convert(pageParam.getParam());
        pageParamR.setParam(capMerchantDO);
        return pageParamR;
    }

    /**
     * 通过主键删除商户
     * @param id 封顶额商户主键
     * @return 影响行数
     */
    public Integer deleteById(Integer id) {
        return capMerchantDomainService.deleteById(id);
    }

    /**
     * 针对某个单独商户生成封顶额
     * @param id 商户表主键
     * @return 该商户的封顶额
     */
    public Integer createCapValue(Integer id) {
        return capMerchantDomainService.createCapValue(id);
    }

    /**
     * 商户批量生成封顶额
     * @param modelId 倍数模型表主键
     * @return 该倍数模型下所有商户总封顶额
     */
    public Integer batchCreateCapValue(Integer modelId) {
        //批量生成封顶额
        Integer capVaue = capMerchantDomainService.batchCreateCapValue(modelId);
        log.info("本次倍数模型id="+modelId+",生成的商户封顶额总和="+capVaue);

        //查询该模型下所有商户封顶额总额
        CapMerchantDO sumDO = new CapMerchantDO();
        sumDO.setModelId(modelId);
        Integer totalCapValue = capMerchantDomainService.getValueSum(sumDO);
        log.info("倍数模型id="+modelId+",商户封顶额总和="+totalCapValue);
        return totalCapValue;
    }

    /**
     * 选择商户list接口
     * @param selectMerchantCmd
     * @return 在租且未被指定倍数模型绑定的商户list
     */
    public List<CapMerchantDTO> selectMerchant(SelectMerchantCommand selectMerchantCmd) {
        //查询指定倍数模型下所有合同签订表主键List
        List<Integer> resourceIdList = capMerchantDomainService.getResourceIdListByModelId(selectMerchantCmd.getCapId());
        log.info("该封顶额活动主键id:"+selectMerchantCmd.getCapId()+",已挂接展位表主键列表="+resourceIdList);

        //远程调用商户模块，查询指定分类（区域）下的在租展位
        List<PropertyResourcesApiDTO> apiDTOList = feignAppService.getRentingResourceListByArea(selectMerchantCmd.getPropertyRelationId());
        List<CapMerchantDTO> merchantDTOList = new ArrayList<>();
        for (PropertyResourcesApiDTO resourcesApiDTO : apiDTOList) {
            //排除已经关联过的展位
            if( !resourceIdList.contains(resourcesApiDTO.getResourcesId()) ){
                CapMerchantDTO capMerchantDTO = new CapMerchantDTO();
                capMerchantDTO.setDayRent(resourcesApiDTO.getDayRentPrice());
                capMerchantDTO.setMerchantShow(resourcesApiDTO.getResourcesName());
                capMerchantDTO.setResourceId(resourcesApiDTO.getResourcesId());
                capMerchantDTO.setShowSpace(resourcesApiDTO.getResourcesConArea());
                merchantDTOList.add(capMerchantDTO);
            }
        }
        return merchantDTOList;
    }

    /**
     * 查询展位区域
     * @return 展位区域list
     */
    public List<PropertyRelatonApiDTO> getShowArea() {
        PropertyRelationApiQuery relationApiQuery = new PropertyRelationApiQuery();
        relationApiQuery.setRelationLevel(EnumPropertyRelationLevel.TWO_LEVEL.getCode()); //只查询各个区域
        return feignAppService.showAreaList(relationApiQuery);
    }
}
